﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.employee
{
    public partial class profile_update : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;
        private Account authinfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            var authsvc = new AuthService();
            try
            {
                if (Session["authinfo"] == null)
                {
                    Response.Redirect("~/employee/login.aspx");
                }
                else
                {
                    authinfo = (Account)Session["authinfo"];
                }
            }
            catch (Exception) { }
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            txtUserName.ReadOnly = true;
            if (!IsPostBack)
            {
                int id = authinfo.AccountId;
                var ob = db.Accounts.First(p => p.AccountId.Equals(id));
                if (ob != null)
                {
                    txtAccountId.Value = ob.AccountId.ToString();
                    txtUserName.Text = ob.UserName;
                    txtEmail.Text = ob.Email;
                    txtFullName.Text = ob.FullName;
                    txtSSN.Text = ob.SSN;
                    txtDob.Text = ob.DoB.ToString("MM/dd/yyyy");
                    cboGender.SelectedValue = ob.Gender.ToString();
                    txtAddress.Text = ob.Address;
                    txtPhone.Text = ob.Phone;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    var ob = db.Accounts.First(p => p.AccountId.Equals(Int32.Parse(txtAccountId.Value)));
                    ob.Email = txtEmail.Text.Trim();
                    ob.FullName = txtFullName.Text.Trim();
                    ob.SSN = txtSSN.Text.Trim();
                    ob.DoB = DateTime.Parse(txtDob.Text.Trim());
                    ob.Gender = Int16.Parse(cboGender.SelectedValue);
                    ob.Address = txtAddress.Text.Trim();
                    ob.Phone = txtPhone.Text.Trim();
                    db.SubmitChanges();
                    Response.Redirect("~/employee/profile_update.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }
    }
}
